//
// Created by Lenovo on 2023/3/7.
//

/* 洛谷 P1048 */

#include <stdio.h>
#include <memory.h>

int Max(int a, int b)
{
    return a > b ? a : b;
}

int main()
{
    int n, m;
    scanf("%d %d", &n, &m);
    int t[m], v[m];
    int dp[n + 1];
    memset(dp, 0, sizeof(int) * (n + 1));
    for (int i = 0; i < m; i++)
    {
        scanf("%d %d", &t[i], &v[i]);
    }

    for (int i = 0; i < m; i++)
    {
        for (int j = n; j >= t[i]; j--)
        {
            dp[j] = Max(dp[j], dp[j - t[i]] + v[i]);
        }
    }
    printf("%d", dp[n]);

    return 0;
}
